Learn R Programming

pbdMPI (version 0.1-5)

apply and lapply: Parallel Apply and Lapply Functions

Description

The functions are parallel versions of apply and lapply functions..

Usage

pbdApply(X, MARGIN, FUN, ..., pbd.mode = c("mw", "spmd"),
           rank.source = .SPMD.CT$rank.root, comm = .SPMD.CT$comm)
  pbdLapply(X, FUN, ..., pbd.mode = c("mw", "spmd"),
            rank.source = .SPMD.CT$rank.root, comm = .SPMD.CT$comm)
  pbdSapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE,
            pbd.mode = c("mw", "spmd"), rank.source = .SPMD.CT$rank.root,
            comm = .SPMD.CT$comm)

Arguments

X
a matrix in pbdApply or a list in pbdLapply and pbdSapply.
MARGIN
MARGIN = 1, 2 as in the apply.
FUN
as in the apply.
...
optional arguments to FUN.
simplify
as in the sapply.
USE.NAMES
as in the sapply.
pbd.mode
mode of distributed data X.
rank.source
a rank of source where X broadcast from.
comm
a communicator number.

Value

  • A list will be returned.

Details

All functions are majorly called in manager/workers mode, and just work the same as their serial version. For SPMD mode, these are redundant even.

If pbd.mode = "mw", the X in rank.source will be redistributed to all processors, then apply FUN on the new data.

If pbd.mode = "spmd", the X is supposed to exist in all processors and just acting as normal apply in SPMD.

References

Programming with Big Data in R Website: http://r-pbd.org/

Examples

Run this code
# Save code in a file "demo.r" and run with 2 processors by
# > mpiexec -np 2 Rscript demo.r

### Initial
library(pbdMPI, quiet = TRUE)
init()
.comm.size <- comm.size()
.comm.rank <- comm.rank()

### Example for pbdApply
N <- 100
x <- matrix((1:N) + N * .comm.rank, ncol = 10)
y <- pbdApply(x, 1, sum)
comm.print(y)

y <- pbdApply(x, 1, sum, pbd.mode = "mw")
comm.print(y)

### Example for pbdLapply
N <- 100
x <- split((1:N) + N * .comm.rank, rep(1:10, each = 10))
y <- pbdLapply(x, sum)
comm.print(unlist(y))

y <- pbdLapply(x, sum, pbd.mode = "mw")
comm.print(unlist(y))

### Finish
finalize()

Run the code above in your browser using DataLab